package com.IODemo;

import java.io.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2022/06/27/14:31
 * @Description:
 * @Target:
 */
public class read {
    
    private static String path =
            "C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\LIST.txt";
    
    public static void main(String[] args) throws Exception {
        readPath(path);
    
    }
    
    static void readPath(String pathTxt) throws Exception{
        try {
            FileReader fileReader = new FileReader(pathTxt);
            BufferedReader bf = new BufferedReader(fileReader);
            // 按行读取字符串
            String str;

            while ((str = bf.readLine()) != null) {
//                System.out.println(str);
                String[] split = str.split("\\\\");
                if (split[split.length-1].contains(".sql") && (split[split.length-1].contains("backup") || split[split.length-1].contains("BACKUP"))){
                    process(str);
                }
            }
            bf.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    static void process(String path) throws Exception{
        String tableName = "ensemble";
 
        FileReader fileReader = new FileReader(path);
        BufferedReader reader = new BufferedReader(fileReader);
        int x = 0;
        String line = null;
        ArrayList<String> strings = new ArrayList<>();
        while ((line = reader.readLine()) != null) {
            if (line.contains("CREATE TABLE")) {
                x++;
                System.out.println(line);
                String[] s1 = line.split(" ");
                for (int i = 5; i < s1.length; i++) {
                    if (s1[i].equals("FROM")) {
                        strings.add(s1[i+1]);
                        break;
                    }
                }
            }
        }
        System.out.println(x);
        System.out.println(strings);
        reader.close();

        String name= strings.toString();//你的录入信息


        if (path.contains("ensemble") || path.contains("ENSEMBLE")){
            tableName = "ensemble";
        }else if (path.contains("esbdata") || path.contains("ESBDATA")){
            tableName = "esbdata";
        }else if (path.contains("oms") || path.contains("OMS")){
            tableName = "oms";
        }else if (path.contains("payment") || path.contains("PAYMENT")){
            tableName = "payment";
        }else if (path.contains("teller9") || path.contains("TELLER9")){
            tableName = "teller9";
        }else if (path.contains("tps0") || path.contains("TPS0")){
            tableName = "tps0";
        }else if (path.contains("glac") || path.contains("GLAC")){
            tableName = "glac";
        }else if (path.contains("om") || path.contains("OM")){
            tableName = "om";
        }else {
            tableName = "other";
        }
        
        switch (tableName){
            case "ensemble":  ensemble(name);break;
            case "esbdata":  esbdata(name);break;
            case "oms":  oms(name);break;
            case "payment":  payment(name);break;
            case "teller9":  teller9(name);break;
            case "tps0":  tps0(name);break;
            case "glac":  glac(name);break;
            case "om":  om(name);break;
            case "other":  other(name);break;
        }
      
    }
    
    
    static void ensemble(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\ensemble.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void esbdata(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\esbdata.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void oms(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\oms.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void payment(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\payment.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void teller9(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\teller9.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }

    static void tps0(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\tps0.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }

    static void glac(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\glac.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void om(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\om.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    static void other(String name){
        File file=new File("C:\\Users\\lenovo\\Desktop\\公司业务总结\\sqlTable\\other.txt");//录入文件地址
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);//false覆盖  true 追加
            byte[] b=name.getBytes();
            os.write(b);//写入
            os.close();//关闭流
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }
}
